package icom.acm.action.mircblog;
/**
 * @author shench
 * 回复类动作
 */
import com.sun.media.protocol.avi.DataSource;

import icom.acm.action.Action;
import icom.acm.bean.T_USERONLINE;
import icom.acm.bean.mircblog.T_MB_MESSAGEREPLY;
import icom.acm.bean.mircblog.VIEW_USER_MESSAGE_REPLY;
import icom.acm.form.mircblog.MessageReplyForm;
import icom.acm.manager.T_UserOnlineMgr;
import icom.acm.manager.mircblog.T_MB_MessageReplyMgr;
import icom.acm.manager.mircblog.View_User_Message_ReplyMgr;
import icom.acm.manager.mircblog_mongo.View_User_Message_ReplyMongoMgr;
import icom.acm.util.DBConnect;
import icom.acm.util.RedisUtil;
import icom.acm.util.json.JSONMsg;
import icom.acm.util.mongo.MongoDB;
import icom.acm.util.redis.RedisDB;
import redis.clients.jedis.Jedis;

public class MessageReplyAction extends Action{		
	private MessageReplyForm form;
	private String toUrl;
	
	public void reply(){
		JSONMsg jmsg = null;
		DBConnect db = new DBConnect();
		Jedis jedis = RedisDB.newInstance().get_Jedis();
		MongoDB mongo = MongoDB.newInstance();
		try {
			T_MB_MessageReplyMgr mmrmgr = new T_MB_MessageReplyMgr();
			View_User_Message_ReplyMgr vumrmgr = new View_User_Message_ReplyMgr();
	//		View_User_Message_ReplyMongoMgr vumrMGmgr = new View_User_Message_ReplyMongoMgr();
			T_UserOnlineMgr uomgr= new T_UserOnlineMgr();
			
			T_MB_MESSAGEREPLY mr = form.toBean();			
			mr = mmrmgr.insert(db, mr);						
			
			VIEW_USER_MESSAGE_REPLY vumr = vumrmgr.selectByMrid(db, mr.getMR_ID());	// 得到回复视图
		//	vumrMGmgr.insert(mongo, vumr);	// 插入到mongo中
			
			T_USERONLINE uo = null;
			if (vumr.getVUMR_R_MR_ID() == vumr.getVUMR_U_ID()){	// 回复了微博消息
				uo = uomgr.selectById(db, vumr.getVUMR_M_U_ID());				
			} else { 		// 回复了回复消息
				uo = uomgr.selectById(db, vumr.getVUMR_R_U_ID());
			}	// 得到被回复的用户
			String sessionid = uo.getUO_SESSIONID();	// 得到用户sessionid值
			String key = RedisUtil.KEY_MESSAGEREPLY2USER_CNT + uo.getUO_U_ID();
			//jedis.rpush(key, mr.getMR_ID().toString());	// 将回复的数据id放入redis队列中
			jedis.incr(key);		// 回复数量加1
			jmsg = new JSONMsg(true, sessionid);	// 将要推送的用户sessionid发送给客户端
			jmsg.include(vumr.toJson());								
		} catch (Exception ex){
			jmsg = new JSONMsg(false, "未知的错误："+ex);
			ex.printStackTrace();
		} finally{			
			db.close();
			if (jmsg != null)
				out.print(jmsg.toString());
		}
	}
	
	public MessageReplyForm getForm() {
		return form;
	}
	public void setForm(MessageReplyForm form) {
		this.form = form;
	}
	@Override
	public String getToUrl() {
		return toUrl;
	}
	@Override
	public void setToUrl(String toUrl) {
		this.toUrl = toUrl;
	}
}
